<?php

class Tourismgoodscategory extends CS_Controller
{
    /*
     * pid parent_id,cid category_id,
     * */
    public function _init()
    {
        $this->load->model('tourism_goods_category_model', 'tourism_goods_category');
        $this->load->library('pagination');
    }

     /**
     *类别列表
     * @auther laona
     * test time 2015.5.28日 级别优化
     */
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismgoodscategory/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismgoodscategory/grid');
        $config['total_rows'] = $this->tourism_goods_category->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_goods_category->page_list($page_num, $num, $getData);
        $data['all_rows']  = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['level'] = array(1=>'一级分类', 2=>'二级分类', 3=>'三级分类');
        $data['class_a'] = $this->input->get('class_a');
        $data['class_b'] = $this->input->get('class_b');
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismgoodscategory/grid', $data);
    }

    /*
     *增加类别
     * @auther laona
     **/
    public function  add()
    {
        $data['level'] = array(1=>'一级分类', 2=>'二级分类', 3=>'三级分类');
        $this->load->view('tourismgoodscategory/add', $data);
    }

    /*
     *增加类别处理
     * @auther laona
     **/
    public function addPost()
    {
        $error = array();
        if ($this->validateParam($this->input->post('name'))) {
            $error[] = '类别名不能为空';
        }
        if ($this->validateParam($this->input->post('level'))) {
            $error[] = '级别不能为空';
        }
        if (!empty($error)) {
            $this->error('tourismgoodscategory/add', '', $error);
        }
        $this->db->trans_start();
        $resultId = $this->tourism_goods_category->insertCategory($this->input->post());
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodscategory/grid', '', '保存成功！');
        } else {
            $this->error('tourismgoodscategory/add', '', '保存失败！');
        }
    }

    /*
     *类别数据更新
     * @auther laona
     **/
    public function edit($id)
    {
        $result = $this->tourism_goods_category->getCategoryById($id);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoodscategory/grid', '', '参数有误！');
        }
        $result = $result->row();
        $data['category'] = $result;
        $parents = ltrim($result->parents, ',0,');
        if (!empty($parents)) {
            $parents = explode(',', $parents, 2);
        }
        $data['class_a'] = isset($parents[0]) ? $parents[0] : '';
        $data['class_b'] = isset($parents[1]) ? $parents[1] : '';
        $data['level'] = array(1=>'一级分类', 2=>'二级分类', 3=>'三级分类');
        $this->load->view('tourismgoodscategory/edit', $data);
    }

    /*
     *更新处理
     * @auther laona
     * */
    public function editPost()
    {
        $error = array();
        if ($this->validateParam($this->input->post('name'))) {
            $error[] = '类别名不能为空';
        }
        if ($this->validateParam($this->input->post('level'))) {
            $error[] = '级别不能为空';
        }
        
        if (!empty($error)) {
            $this->error('tourismgoodscategory/edit', $this->input->post('id'), $error);
        }
        $this->db->trans_start();
        $resultId = $this->tourism_goods_category->updateCategory($this->input->post());
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodscategory/grid', '', '修改成功！');
        } else {
            $this->error('tourismgoodscategory/grid', $this->input->post('id'), '修改失败！');
        }
    }
    
    /**
     * 根据ajax反馈的父id，返回所有子集 （json格式）
     */
    public function select_children($parent_id=0)
    {
        $childrenData = array();
        if ($parent_id) {
            $childrenData = $this->tourism_goods_category->getCategoryLevel($parent_id);
        }
        echo json_encode($childrenData);exit;
    }
    
    public function delete($id)
    {
        $result = $this->tourism_goods_category->getCategoryById($id);
        if ($result->num_rows() <= 0) {
            $this->error('tourismgoodscategory/grid', '', '参数有误！');
        }
        $result = $result->row();
//      if ($result->level != 3) {
//          $this->error('tourismgoodscategory/grid', '', '此类别不可删除。');
//      }
        $this->db->trans_start();
        $resultId = $this->tourism_goods_category->delete($id);
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodscategory/grid', '', '删除成功！');
        } else {
            $this->error('tourismgoodscategory/grid', $this->input->post('id'), '删除失败！');
        }
    }
}